-module( kinematics_cell ).

-export( [start/1] ).


start( Bounds ) ->
  error_logger:info_msg( "Cell ~p started at ~p\n", [self(), Bounds] ),
  loop( Bounds, [] ).



loop( Bounds, Objects ) ->
  receive
    { add_object, ObjPid } ->
      error_logger:info_msg( "Adding object ~p to cell ~p\n", [ ObjPid, self() ] ),
      ObjPid ! { object_added, self() },
      loop( Bounds, [ObjPid | Objects] );
      
    { remove_object, ObjPid } ->
      error_logger:info_msg( "Removing object ~p from cell ~p\n", [ ObjPid, self() ] ),
      ObjPid ! { object_removed, self() },
      loop( Bounds, lists:delete( ObjPid, Objects ) );

    { propagate, Msg } ->
      [ ObjPid ! Msg || ObjPid <- Objects ],
      loop( Bounds, Objects );

    M ->
      error_logger:error_msg( "Cell unexpected message: ~p\n", [M] ),
      loop( Bounds, Objects )
  end.
